﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Memory;
using Elsa.Workflows.Models;
using Newtonsoft.Json;
using T.FAS.Runtime.MF.Activities;
using T.FAS.Runtime.MF.Activities.Helper;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.SPI;
using ElsaExpression = Elsa.Expressions.Models.Expression;
namespace T.FAS.Runtime.MF.Activities.Nodes
{
    // 开始节点
    public class StartNode : NodeBase, INodeMapper
    {
        [JsonProperty("data")]
        public new StartNodeData Data { get; set; }
        public List<Variable> Variables { get; set; } = new List<Variable>();
        public List<OutputDefinition> Outputs { get; set; }
        public List<InputDefinition> Inputs { get; set; } = new List<InputDefinition>();
        public IActivity MapToActivity()
        {
            var activity = new FasStartActivity() { Id = Id, Name = Data.Label };

            if (Data.UserVariables != null && Data.UserVariables.Count() > 0)
            {
                foreach (var varItem in Data.UserVariables)
                {
                    SetVariableInfo(varItem);
                    //varItem.CompositeVarName = $"{Id}:{varItem.VariableName}";
                    //varItem.CompositeVarName = varItem.CompositeVarName.Replace("-", "").Replace(":", "");
                    var variable = VariableHelper.GenerateVariable(varItem);
                    Variables.Add(variable);

                    //将开始节点的入参做为整个工作流的输入参数
                    Inputs.Add(new InputDefinition { Type = typeof(object), Name = varItem.VariableName, Description = varItem.Description });
                }
            }
            activity.Metadata = DesignerMeta;

            //处理入参
            ProcessInputs(activity);
            ////获取所有可能的输出端口
            //activity.Outcomes = Ports.Items.Where(t => t.Group.ToLower() == "out").Select(t=>t.Id);

          
            return activity;
        }
        private void ProcessInputs(FasStartActivity activity)
        {
            if (Data.UserVariables != null && Data.UserVariables.Count() > 0)
            {
                activity.Inputs = new List<BranchCondition>();
                foreach (var varItem in Data.UserVariables)
                {
                    activity.Inputs.Add(new BranchCondition { Label= varItem.VariableName, Condition= ElsaExpression.LiteralExpression(null) });
                }
            }
        }
        public override List<UserVariable> NodeInputOutputVariables
        {
            get
            {
                if (Data.UserVariables == null) return new List<UserVariable>();
                Data.UserVariables.ForEach(v =>
                {
                    SetVariableInfo(v);
                });
                return Data.UserVariables;
            }
        }
        private void SetVariableInfo(UserVariable v)
        {
            if (string.IsNullOrEmpty(v.ActivityID))
            {
                v.ActivityID = Id;
                v.CompositeVarName = v.VariableName;
            }

        }
    }
   

    // 节点数据类
    public class StartNodeData
    {
        [JsonProperty("label")]
        public string Label { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("inputFields")]
        public List<UserVariable> UserVariables { get; set; }
    }
}
